package com.airspyar;

import java.util.Vector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RadarView extends View {
	private final static float PLANE_MAX_ALTITUDE=13;
	private float indication=0;
	private ObserverDataHolder observerDataHolder=null;
	private PlanesDataProvider planesDataProvider=null;
	private Bitmap bitmap;
	private Bitmap indicator;
	private Bitmap shadow;
	private Paint paint;
	private float halfR;
	
	public RadarView(Context ctx, AttributeSet attrs){
		super(ctx, attrs);
		bitmap=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.radar);
		indicator=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.radarindicator);
		shadow=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.radarshadow);
		
		paint=new Paint();
		paint.setFilterBitmap(true);
		paint.setAntiAlias(true);
	}
	
	public void setDataProviders(ObserverDataHolder observerDataHolder, PlanesDataProvider planesDataProvider){
		this.observerDataHolder=observerDataHolder;
		this.planesDataProvider=planesDataProvider;
	}
	
	@Override
	public void onSizeChanged(int w, int h, int oldw, int oldh){
		bitmap=Bitmap.createScaledBitmap(bitmap, w, w, true);
		indicator=Bitmap.createScaledBitmap(indicator, w, w, true);
		shadow=Bitmap.createScaledBitmap(shadow, w, w, true);
		
		halfR=bitmap.getWidth()/2.0f;
	}
	
	private void updateIndication(){
		Vector3d orientationVector=observerDataHolder.getOrientationVector();
			
		if(orientationVector==null)
			return;
		
		float targetIndication=-((float)Math.toDegrees(Math.atan2(orientationVector.getY(), orientationVector.getX()))-90);
		
		float diff=targetIndication-indication;
		if(diff>180)
			diff-=360;
		else if(diff<-180)
			diff+=360;
		
		diff/=3.0f;
		
		indication+=diff;				
		if(indication>180)
			indication-=360;
		else if(indication<-180)
			indication+=360;
		
	}
	
	private void drawRadar(Canvas c){
		paint.setAlpha(210);
		c.drawBitmap(shadow, 0, 0, paint);
		c.save();
		c.rotate(-indication, halfR, halfR);
		c.drawBitmap(bitmap, 0, 0, paint);
		
		Vector<FlightState> planes=planesDataProvider.getPlanes();
		
		
		if(planes!=null){
			for(FlightState plane : planes){
				Vector3d distVector=plane.getUpdatedDistVector();
				if(distVector.lenght()>planesDataProvider.getVisibilityDistance())
					continue;
				
				float x=halfR*(distVector.getX()/(float)planesDataProvider.getVisibilityDistance());
				float y=halfR*(distVector.getY()/(float)planesDataProvider.getVisibilityDistance());
				x*=0.7f;
				y*=0.7f;
				
				float gb=0xff-0xff*(plane.getLocation().getAltitude()/PLANE_MAX_ALTITUDE);
				if(gb<0)
					gb=0; 
				else if(gb>0xff)
					gb=0xff;
				
				paint.setARGB(0xe0, 0xff, (int)gb, (int)gb);
				c.drawCircle(halfR+x, halfR-y, 4, paint);
			}
		}

		c.restore();
		paint.setAlpha(255);
		c.drawBitmap(indicator, 0, 0, paint);
	}
	
	@Override
	public void onDraw(Canvas c){
		if(planesDataProvider!=null && observerDataHolder !=null){
			updateIndication();
			drawRadar(c);
		}
		
		this.postInvalidate();
	}
}
